package reflect1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class MyREflect {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        yoona y=new yoona();
        
        //Class.forName("reflect1.Student");
        //最常用的
        Class<?> clazz = Class.forName("reflect1.Student");
        //System.out.println(clazz);
        //一般更多的当做参数进行传递
       /* Class<Student> clazzz = Student.class;
        //System.out.println(clazzz);
        Student s=new Student();
        //当我们已经有了这个类的对象时才可以使用
        Class clazzzz = s.getClass();
        System.out.println(clazzzz);*/

        //获取构造方法(不包括私有的)
      /*  Constructor<?>[] constructors = clazz.getConstructors();
        for(Constructor con:constructors){
            System.out.println(con);

        }*/
        //获取所有的构造方法（包括私有）
       /* Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for(Constructor con:constructors){
            System.out.println(con);

        }
*/
    /*    Constructor<?> con1 = clazz.getDeclaredConstructor();
        System.out.println(con1);
        Constructor<?> con2 = clazz.getDeclaredConstructor(int.class);
        System.out.println(con2);*/
        Constructor<?> con3 = clazz.getDeclaredConstructor(String.class);
        System.out.println(con3);
        Constructor<?> con4 = clazz.getDeclaredConstructor(String.class,int.class);
        System.out.println(con4);
/*
        int modifiers = con4.getModifiers();
        System.out.println(modifiers);*/

        con4.setAccessible(true);//暴力反射：表示临时取消权限校验
        Student stu =(Student) con4.newInstance("张三", 23);
        System.out.println(stu);

        Object o = con3.newInstance("lisi");
        System.out.println(o);

    }
}
